package com.sunda.spmsweb.ordercontroller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmscommon.feign.IFeignServiceCommon;
import com.sunda.spmsorder.service.IOrderLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单审批记录表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/orderLog")
@Api(tags = "请购单日志查询接口", description = "请购单日志查询接口")
public class OrderLogController {

    @Autowired
    IOrderLogService iOrderLogService;

    @Autowired
    IFeignServiceCommon iFeignServiceCommon;

    @RequestMapping("/getOrderLog")
    @ApiOperation(value = "根据请购单号查询日志", notes = "根据请购单号查询日志\n" +
            "示例 请购单号必输：spmsWsId = GH011002-20210203-0004\n", httpMethod = "POST")
    public ResponseResult getOrderWsDtl(String spmsWsId){
        try {
            return ResponseResult.success().add("orderLogList", iOrderLogService.orderLogList(spmsWsId));
        }catch (Exception e){
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOrderLogSrm")
    @ApiOperation(value = "根据请购单号类型查询SRM日志", notes = "根据请购单号类型查询SRM日志\n" +
            "根据车间请购单号 或 工厂请购单号查询请购单据在SRM状态。请求的单据必须是 OA审批通过提交SRM 的单据，即关联的" +
            "工厂请购单 工厂请购单SPMS状态 = 5，其余状态忽略。\n" +
            "orderType：1车间请购单，2工厂请购单；orderUuid 对应单号 UUID；\n" +
            "示例 orderType = 2；orderUuid = 819eec3ca6a04548806e46d2ac742f07；\n" +
            "orderType = 1；orderUuid = 898388417b634b5387280ec773bce7e6 或 793598e3d20243de83c375f24b491599；", httpMethod = "POST")
    public ResponseResult getOrderLogSrm(String orderType, String orderUuid){
        try {
            List<Map<String, Object>> orderWerksInfo = iOrderLogService.getOrderLogSrm(orderType, orderUuid);
            if (orderWerksInfo == null){
                return ResponseResult.error("您所查询的单据无SRM日志");
            }
            String received = iFeignServiceCommon.sendRestToSapCommon(iOrderLogService.formatDataSend(orderWerksInfo));
            return ResponseResult.success().add("sendRestMsg", iOrderLogService.formatDataSend(orderWerksInfo))
                    .add("orderLogSrm", iOrderLogService.formatDataReceived(received, orderWerksInfo));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOrderLogList")
    @ApiOperation(value = "根据UUID查询日志", notes = "根据车间或工厂请购单UUID查询日志\n" +
            "根据车间或工厂请购单UUID查询日志，示例\n" +
            "uuid = cbff183779454485a3351edff6ee9e57, ad5eb5e004d14933947251756f11828e", httpMethod = "POST")
    public ResponseResult getOrderLogList(String uuid){
        try {
            if (StringUtils.isEmpty(uuid)){
                return ResponseResult.error("请求参数不能为空");
            }
            return ResponseResult.success().add("orderLogList", iOrderLogService.getOrderLogList(uuid));
        }catch (Exception e){
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

}
